<? 
// Page variables
$rightCol   = 1;
$parseURI   = 1;
$loggedUser = 0;

include("includes/functions.php"); 

// Get page header
if (!empty($mainCat) && empty($subCat)) {
	// Get Main Cat Title
	$sql = "SELECT * from transLevels WHERE Path = '$mainCat'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	if (mysql_num_rows($result)<1) {
		header("Location: http://www.tcnTransition.org/tcnError.php"); 
	}
	
	$parentID   = $mainCat;
	$pageHead   = stripslashes($row[LevelName]);
	
	// Get sub categories
	$sqlSub = "SELECT * from transLevels WHERE Path LIKE '$mainCat/%' AND SubCat = '1' AND InMainMenu = '1' AND Active = '1' ORDER BY DisplayOrder";
	$resultSub = mysql_db_query($dbname,$sqlSub); 
} elseif (!empty($subCat)) {
	// Get sub category details
	$subCatPath = $mainCat.$subCat;
	$sql = "SELECT * from transLevels WHERE Path = '$subCatPath'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	if (mysql_num_rows($result)<1) {
		header("Location: http://www.tcnTransition.org/tcnError.php"); 
	}
	
	$parentID   = $row[ParentID];
	$subCatNamePage = stripslashes($row[LevelName]);
		
	// Get main category
	$sql = "SELECT * from transLevels WHERE Path = '$mainCat'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$catParentID = $parentID;
	$pageHead = stripslashes($row[LevelName]);
	
	$subCatSQLPath = $mainCat.$subCat;
} else {
}

include("includes/header.php"); ?>
		<div id="pageTop"></div>
		<div id="pageContent">
		
		<? include("includes/menu_main.php"); ?>	
		
		<div id="rightCol">
			<img src="http://www.tcnTransition.org/images/rightColTop.gif">
			<? include("includes/rightCol.php"); ?>
		</div>
		
		<div id="mainContent">
			<?			
			if (!empty($mainCat) && empty($subCat)) {
				echo "<h1>$pageHead</h1>\n";
			} elseif (!empty($subCat)) {
				//echo "<h1><a href=\"".$siteURL.$mainCat."\">$pageHead</a> : $subCatNamePage</h1>\n";
				echo '<h1><a href="'.$siteURL.'/resourceProcess.php?c='.$mainCat.'">'.$pageHead.'</a> : '.$subCatNamePage.'</h1>';
			}
			?>
			
			<p class="margin">
			
			<?
			if (!empty($mainCat) && empty($subCat)) {
				while ($valueSub = mysql_fetch_assoc($resultSub)) {
					$subID = $valueSub[LevelID];
					$subCatName = stripslashes($valueSub[LevelName]);
					$subPath = $valueSub[Path];
					echo "<h2>$subCatName</h2>\n";
					
					// Get 3rd level categories
					$sqlSub2 = "SELECT * from transLevels
							WHERE 
							Path LIKE '$subPath/%' AND
							Path <> '$subPath'";
					$resultSub2 = mysql_db_query($dbname,$sqlSub2);
					echo "<p class=\"margin\">\n";
					echo "<ul class=\"ulMain\">\n";
					while ($valueSub2 = mysql_fetch_assoc($resultSub2)) {
						$sub2ID   = $valueSub2[LevelID];
						$sub2Cat  = stripslashes($valueSub2[LevelName]);
						$sub2Path = stripslashes($valueSub2[Path]);
						
						// Get number of resources for this sub category
						//$sqlNR = "SELECT LinkID from transLinks WHERE Path = '$sub2Path' AND Status = '1'";
												
						$sqlNR = "SELECT LinkID from transLinkPath WHERE PathID = '$sub2ID'";
						$resultNR = mysql_db_query($dbname,$sqlNR);
						$rowsNR = mysql_num_rows($resultNR);
						
						//echo "<li><b><a href=\"".$siteURL.$sub2Path."\">$sub2Cat</a></b> <span class=\"small\">[$rowsNR Resources]</span></li>\n";
						echo '<li><b><a href="'.$siteURL.'/resourceProcess.php?c='.$sub2Path.'">'.$sub2Cat.'</a></b> <span class="small">['.$rowsNR.' Resources]</span></li>';
					}
					echo "</ul>\n";
					echo "</p>\n";
				}
			} elseif (!empty($subCat)) {
				// Get all sub-sub categories
				$sqlSub2 = "SELECT * from transLevels 
							WHERE 
							Path LIKE '$subCatSQLPath/%'
							ORDER BY DisplayOrder";
				$resultSub2 = mysql_db_query($dbname,$sqlSub2);
				echo "<p class=\"margin\">\n";
				echo "<ul class=\"ulMain\">\n";
				while ($valueSub2 = mysql_fetch_assoc($resultSub2)) {
					$sub2ID   = $valueSub2[LevelID];
					$sub2Cat  = stripslashes($valueSub2[LevelName]);
					$sub2Path = stripslashes($valueSub2[Path]);
					
					// Get number of resources for this sub category
						//$sqlNR = "SELECT LinkID from transLinks WHERE Path = '$sub2Path' AND Status = '1'";
						
						$sqlNR = "SELECT LinkID from transLinkPath WHERE PathID = '$sub2ID'";
						$resultNR = mysql_db_query($dbname,$sqlNR);
						$rowsNR = mysql_num_rows($resultNR);
					
					//echo "<li><b><a href=\"".$siteURL.$sub2Path."\">$sub2Cat</a></b> <span class=\"small\">[$rowsNR resources]</span>&nbsp;&nbsp;";
					echo '<li><b><a href="'.$siteURL.'/resourceProcess.php?c='.$sub2Path.'">'.$sub2Cat.'</a></b> <span class="small">['.$rowsNR.' resources]</span>&nbsp;&nbsp;';
					
					if (!empty($sub2CatDesc)) {	
						//echo "<br><span class=small>$sub2CatDesc</span>\n"; 
					}
					echo "</li>\n";
				}
				echo "</ul>\n";
				echo "</p>\n";
				
			}
			?>
			<div class="spacer2" style="height: 700px;"></div>
			</p>
		</div> <!-- End MainContent -->
		
		
	</div> <!-- End Page Content -->
<? include("includes/footer.php"); ?>